package com.hp.appfw.util;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Date;

public class ClassUtil {
	public static String getClassInfo(Object model) {
		StringBuffer info = new StringBuffer();
		
		try{
			info.append("Class Name:" + model.getClass().getName() + "\r\n");
			Field[] field = model.getClass().getDeclaredFields(); 
			
			for (int j = 0; j < field.length; j++) {
				String name = field[j].getName(); 
				name = name.replaceFirst(name.substring(0, 1),name.substring(0, 1).toUpperCase()) ;
				info.append(name + ":");
				String type = field[j].getGenericType().toString(); 
				if (type.equals("class java.lang.String")) { 
					Method m = model.getClass().getMethod("get" + name);
					String value = (String) m.invoke(model); 
					if (value != null) {
						info.append(value + "\r\n");
					}
					else
					{
						info.append("null\r\n");
					}
				}
				if (type.equals("class java.lang.Integer")) {
					Method m = model.getClass().getMethod("get" + name);
					Integer value = (Integer) m.invoke(model);
					if (value != null) {
						info.append(value + "\r\n");
					}
					else
					{
						info.append("null\r\n");
					}
				}
				if (type.equals("class java.lang.Short")) {
					Method m = model.getClass().getMethod("get" + name);
					Short value = (Short) m.invoke(model);
					if (value != null) {
						info.append(value + "\r\n");
					}
					else
					{
						info.append("null\r\n");
					}
				}
				if (type.equals("class java.lang.Double")) {
					Method m = model.getClass().getMethod("get" + name);
					Double value = (Double) m.invoke(model);
					if (value != null) {
						info.append(value + "\r\n");
					}
					else
					{
						info.append("null\r\n");
					}
				}
				if (type.equals("class java.lang.Boolean")) {
					Method m = model.getClass().getMethod("get" + name);
					Boolean value = (Boolean) m.invoke(model);
					if (value != null) {
						info.append(value + "\r\n");
					}
					else
					{
						info.append("null\r\n");
					}
				}
				if (type.equals("class java.util.Date")) {
					Method m = model.getClass().getMethod("get" + name);
					Date value = (Date) m.invoke(model);
					if (value != null) {
						info.append(value + "\r\n");
					}
					else
					{
						info.append("null\r\n");
					}
				}
			}
		}
		catch(Exception e)
		{
			info.append(e.toString() + e.getMessage());
		}
		return info.toString();
	}
}
